import sdk from 'sdk'
import {encodeQuery} from 'utils/helper/query'
import {isObject} from 'utils/helper/validator'
import city from '@/mixins/city'

export default {
  data() {
    return {
      selectCityFlag: false // 标记是否已进入城市选择页面
    }
  },
  mixins: [city],
  onShow() {
		this.checkSelectedCity()
	},

  methods: {
    $listSearch_toSearch(params) {
			let url = encodeQuery(params)
			sdk.navigateTo({
				url: '/pages/search/index?' + url
			})
    }, 
	$listSearch_toSelectCity(params={}) {
		this.selectCityFlag = true
		let url = encodeQuery({
			...params,
			location: 1,
			history: 1,
			hot: 1
		})
		sdk.navigateTo({
			url: '/pages/select-city/index?' + url
		})
	},
		
    /**
		 * 检测选中城市
		 */
	checkSelectedCity() {
      // 确保params已定义
        if (!this.params && isObject(this.params)) return

		if (!this.selectCityFlag)  return

		const cacheCity = this.$city_getCacheCity()
		
		sdk.getSelectCity().then(res => {
			if (!res.name) return // 未选中任何城市
			
			let { regionId, name } = res			
			regionId && (this.params.regionId = regionId)
			name && (this.params.name = name)
			this.params.isCurrentCity = false
			if(cacheCity){
				const { lat, lon } = cacheCity
				lat && ( this.params.lat = lat )
				lon && ( this.params.lon = lon )
				if(Number(regionId) === Number(cacheCity.regionId)) this.params.isCurrentCity = true
			}			
		})
	}
  }
}